<?php
	// Return the requested graphic file to the browser
	// or a 304 code to use the cached browser copy
	$fileModTime = filemtime($graphicFileName);
	// Getting headers sent by the client.
	$headers = getRequestHeaders();

	//If the url is requesting styles
	if ( isset( $_GET['style'] ) ) {
		//Set the header to a stylesheet
		header('Content-type: text/css'); 
		echo '@charset "utf-8";';
		$params = $_GET['style'];
		$params = explode("|",$params);
		
		//For each paramater that's been split up, check for layout, 
		//reset, tags and ie6 within that parameter, and if it's there,
		//strip out any unnecessary chars and concatinate it into the 
		//output.
		foreach ( $params as $item ) {
			if ($item == "layout") {
				echo strip_space( file_get_contents('css/layout.css'),1,1 );
			}
			
			if ($item == "reset") {
				echo strip_space( file_get_contents('css/reset.css'),1,1 );
			}
			
			if ($item == "tags") {
				echo strip_space(  file_get_contents('css/tags.css'),1,1 );
			}
			
			if ($item == "ie6"){
				echo strip_space(  file_get_contents('css/ie6.css'),1,1 );
			}
		}
	}
	
	//If the url is requesting Javascript
	if ( isset( $_GET['js'] ) ) {
		//Set the header to Javascript
		header('Content-type: text/javascript');
		$params = $_GET['js'];
		$params = explode("|",$params);
		
		//For each paramater that's been split up, check for jquery, 
		//jqueryflow, cufon and ie6 within that parameter, and if it's there,
		//strip out any unnecessary chars and concatinate it into the 
		//output.
		foreach( $params as $item ) {
			if ($item == "jquery") {
				echo strip_space( file_get_contents('javascript/jquery.js') );
			}
			if ($item == "jqueryflow") {
				echo strip_space( file_get_contents('javascript/jquery.flow.1.2.min.js') );
			}
			if ($item == "cufon") {
				echo strip_space( file_get_contents('javascript/cufon-yui.js') );
				echo strip_space( file_get_contents('javascript/JustAnotherStamp-400.font.js') );
				echo "Cufon.replace('h1, h2');";
			}
			if ($item == "ie6") {
				echo strip_space( file_get_contents('javascript/dd_png.js') );
				echo strip_space( file_get_contents('javascript/ie6.js') );
			}
		}
	} 
	
	function isBuggyIe() {
		$ua = $_SERVER['HTTP_USER_AGENT'];
		// quick escape for non-IEs
		if (0 !== strpos($ua, 'Mozilla/4.0 (compatible; MSIE ')
			|| false !== strpos($ua, 'Opera')) {
			return false;
		}
		// no regex = faaast
		$version = (float)substr($ua, 30);
		return (
			$version < 6
			|| ($version == 6  && false === strpos($ua, 'SV1'))
		);
	}
	
	//Strip unneccesary chars from input.
	function strip_space($input, $stripcomments=null, $strip_charset=null){
		if (isset($stripcomments)){
			$return = str_replace( "\n", "", str_replace( "\t", "", strip_comments( $input ) ) );
		} else {
			$return = str_replace( "\n", "", str_replace( "\t", "", $input ) );
		}
		
		if (isset($strip_charset)){
			$return = strip_charset($return);
		}
		
		return $return;
		
	}
	
	//Remove Charset from a CSS file
	function strip_charset($input){
		return str_replace('@charset "utf-8";','', $input);
	}
	
	//Strip Comments from a file
	function strip_comments($input){
		$pattern = '/\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*\/+/'; //Looks for comments over many lines
		return preg_replace( $pattern , "", $input );
	}
	
	// return the browser request header
	// use built in apache ftn when PHP built as module,
	// or query $_SERVER when cgi
	function getRequestHeaders() {
	  if (function_exists("apache_request_headers")) {
		if($headers = apache_request_headers()) {
		  return $headers;
		}
	  }
	  $headers = array();
	  // Grab the IF_MODIFIED_SINCE header
	  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
		$headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
	  }
	  return $headers;
	}

?>
